package com.kede.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kede.dto.AjaxMessage;
import com.kede.dto.LayuiTableData;
import com.kede.entity.Agent;
import com.kede.entity.Customer;
import com.kede.entity.User;
import com.kede.service.AgentService;
import com.kede.service.CustomerService;
import com.kede.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.jws.WebParam;
import java.sql.Timestamp;
import java.time.LocalDateTime;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 【请填写作者】
 * @since 2020-04-03
 */
@Controller
public class AgentController {

    @Autowired
    AgentService agentService;

    @Autowired
    UserService userService;

    @Autowired
    CustomerService customerService;

    @GetMapping("/agentlist")
    public String agent(){
        return "agent/agentlist";
    }

    @ResponseBody
    @GetMapping("list")
    public LayuiTableData list(
            String name,
            @RequestParam(value = "page", defaultValue = "1") Integer currentPage,
            @RequestParam(value = "limit", defaultValue = "10") Integer limit
    ) {
        QueryWrapper<Agent> wrapper = new QueryWrapper<>();
        wrapper.like("name",name);
        wrapper.eq("query", 1);
        Page<Agent> query = new Page<Agent>();
        query.setCurrent(currentPage);
        query.setSize(limit);

        Page<Agent> page = agentService.page(query, wrapper);
        LayuiTableData data = new LayuiTableData();
        data.setData(page.getRecords());
        data.setCount(page.getTotal());

        return data;
    }

    @GetMapping("/agentinfo")
    public String index(Integer id,Model model){
        Agent agent = agentService.getById(id);
        model.addAttribute("agent", agent);

        return "agent/agentinfo";
    }

    @ResponseBody
    @PostMapping("/agentinfo")
    public AjaxMessage update(Agent agent){
        LocalDateTime now = LocalDateTime.now();
        agent.setCreatetime(Timestamp.valueOf(now));
        AjaxMessage message = new AjaxMessage();
        if (agentService.updateById(agent)){
            message.setCode(0);
            message.setMsg("更新成功");
        }else {
            message.setCode(1);
            message.setMsg("更新失败");
        }
        return message;
    }

    @GetMapping("/detail")
    public String detail(Integer id,Model model){
        Agent agent = agentService.getById(id);
        model.addAttribute("agent", agent);
        return "agent/agentdetail";
    }



    @ResponseBody
    @PostMapping("/delete")
    public AjaxMessage delete(Integer id){
        AjaxMessage message = new AjaxMessage();
        if(agentService.removeById(id)){
            message.setCode(0);
            message.setMsg("删除成功");
        }else {
            message.setCode(1);
        }
        return message;
    }


    @GetMapping("/add")
    public String add(){
        return "agent/agentadd";
    }

    @ResponseBody
    @PostMapping("agent/add")
    public AjaxMessage add(Agent agent){
        AjaxMessage message = new AjaxMessage();
        if (agentService.save(agent)){
            message.setCode(0);
            message.setMsg("添加成功");
        }else {
            message.setCode(1);
        }
        return message;
    }

    @GetMapping("agent/customers")
    public String customer(
    ){
        return "agent/customers";
    }

    @ResponseBody
    @GetMapping("cuslist")
    public LayuiTableData cuslist(
            String name,
           @RequestParam(value = "page",defaultValue = "1") Integer currentPage,
           @RequestParam(value = "limit",defaultValue = "10") Integer limit
    ){
        QueryWrapper<Customer> wrapper = new QueryWrapper<>();
        wrapper.like("name", name);
        wrapper.eq("query", 1);

        Page<Customer> query = new Page<>();
        query.setCurrent(currentPage);
        query.setSize(limit);

        Page<Customer> page = customerService.page(query, wrapper);
        LayuiTableData data = new LayuiTableData();
        data.setData(page.getRecords());
        data.setCount(page.getTotal());

        return data;
    }


}
